package com.diankangwo.factory.controller;

import com.diankangwo.common.annotation.SysLog;
import com.diankangwo.common.utils.PageUtils;
import com.diankangwo.common.utils.Query;
import com.diankangwo.common.utils.R;
import com.diankangwo.factory.annotation.LoginUser2;
import com.diankangwo.factory.entity.TdCommodity;
import com.diankangwo.factory.entity.TdOrder;
import com.diankangwo.factory.entity.TdOrderShoppingcart;
import com.diankangwo.factory.entity.TdShoppingcart;
import com.diankangwo.factory.service.*;
import com.diankangwo.sys.entity.SysUserEntity;
import com.diankangwo.util.PayStatus;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 订单Controller
 */
@RestController
@RequestMapping("/fac/order")
public class TdOrderController {

    @Autowired
    private TdUserInfoService tdUserInfoService;

    @Autowired
    private TdOrderService tdOrderService;
    @Autowired
    private TdCommodityService tdCommodityService;
    @Autowired
    private TdOrderShoppingcartServices tdOrderShoppingcartServices;
    @Autowired
    private TdShoppingcartServices tdShoppingcartServices;
    @Autowired
    private TdExpressCodeService tdExpressCodeService;

    /**
     * 根据条件获取订单列表
     */
    @ApiOperation(value = "根据条件获取订单列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "查询条件", required = true, dataType = "Map<String,Object>"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态", required = true, dataType = "String")
    })
    @RequestMapping("/list")
    @RequiresPermissions("fac:order:list")
    public R list(@LoginUser2 SysUserEntity sysUserEntity, @RequestParam Map<String, Object> params, String orderStatus) {
        //查询列表数据
        params.put("orderStatus", orderStatus);
        Query query = new Query(params);
        List<TdOrder> tdOrders = tdOrderService.queryList(query);
        int total = tdOrderService.queryTotal(query);
        List<TdOrder> tdOrders2 = new ArrayList<>();
        for (int i = 0; i < tdOrders.size(); i++) {
            List<TdOrderShoppingcart> tdOrderShoppingcart = tdOrderShoppingcartServices.selectByOrderId(tdOrders.get(i).getId());
            for (int j = 0; j < tdOrderShoppingcart.size(); j++) {
                TdShoppingcart tdShoppingcart = tdShoppingcartServices.selectByPrimaryKey(tdOrderShoppingcart.get(j).getShoppingcartId());
                TdCommodity tdCommodity = tdCommodityService.selectByPrimaryKey(tdShoppingcart.getCommodityid());
                if (sysUserEntity.getTdFactoryid() == Integer.parseInt(tdCommodity.getFactoryid()) || sysUserEntity.getTdFactoryid() == -1) {
                    tdOrders2.add(tdOrders.get(i));
                    continue;
                }
            }
        }
        //查询订单中对应购物车选择的属性
        for (int i = 0; i < tdOrders2.size(); i++) {
            List<TdShoppingcart> tdShoppingcartList = new ArrayList<>();
            List<TdOrderShoppingcart> tdOrderShoppingcart = tdOrderShoppingcartServices.selectByOrderId(tdOrders2.get(i).getId());
            for (int j = 0; j < tdOrderShoppingcart.size(); j++) {
                tdShoppingcartList.add(tdShoppingcartServices.selectByPrimaryKey(tdOrderShoppingcart.get(j).getShoppingcartId()));
            }
            tdOrders2.get(i).setTdShoppingcart(tdShoppingcartList);
        }
        PageUtils pageUtil = new PageUtils(tdOrders2, tdOrders2.size(), query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查询单个订单详情
     */
    @ApiOperation(value = "查询单个订单详情", notes = "")
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "int")
    @SysLog("查询单个订单详情")
    @RequestMapping("/findById")
    @RequiresPermissions("fac:order:findById")
    public R findById(int id) {
        TdOrder tdOrder = tdOrderService.selectByPrimaryKey(id);
        return R.ok().put("order", tdOrder);
    }

    /**
     * 发货
     *
     * @param id
     * @param wuliuType
     * @param wuliuOrderNo
     * @return
     */
    @ApiOperation(value = "发货", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "int"),
            @ApiImplicitParam(name = "wuliuType", value = "物流公司", required = true, dataType = "String"),
            @ApiImplicitParam(name = "wuliuOrderNo", value = "物流订单号", required = true, dataType = "String")
    })
    @SysLog("发货")
    @RequestMapping("/shipments")
    @RequiresPermissions("fac:order:shipments")
    public R shipments(int id, String wuliuType, String wuliuOrderNo) {
        TdOrder tdOrder = tdOrderService.selectByPrimaryKey(id);
        tdOrder.setWuliuOrderNo(wuliuOrderNo);
        tdOrder.setWuliuType(wuliuType);
        tdOrder.setOrderStatus(PayStatus.delivering.getName());
        tdOrder.setUpdateDate(new Date());
        tdOrderService.updateByPrimaryKey(tdOrder);
        return R.ok().put("order", tdOrder);
    }

    /**
     * 查询物流公司
     *
     * @return
     */
    @ApiOperation(value = "查询物流公司", notes = "")
    @SysLog("查询物流公司")
    @RequestMapping("/ExpressCode")
    @RequiresPermissions("fac:order:ExpressCode")
    public R ExpressCode() {
        return R.ok().put("ExpressCode", tdExpressCodeService.queryList());
    }

    /**
     * 删除订单
     *
     * @return
     */
    @ApiOperation(value = "删除订单", notes = "")
    @SysLog("删除订单")
    @RequestMapping("/delete")
    @RequiresPermissions("fac:order:delete")
    public R delete(int id) {
        TdOrder tdOrder = tdOrderService.selectByPrimaryKey(id);
        if (tdOrder.getOrderStatus().equals("unpaid")) {
            tdOrderService.deleteByPrimaryKey(id);
        }
        return R.ok();
    }
}
